package com.test.demo;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

public class BossAuto {

    private WebDriver driver;
    private WebDriver.Navigation navigation;
    private static final String URL1 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=ae6219265d527daa1XF92dm0FFQ~&status=0";
    private static final String URL2 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=201b0cbab84abfdf1XF92dm7GFo~&status=0";
    private static final String URL3 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=e33cea2dd420a0261XF92dm-ElE~&status=0";
    private static final String URL4 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=e65384f839f5825a1n193dy1EVA~&status=0";
    private static final String URL5 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=f7f1558faefa8e6c1n193dy4FFM~&status=0";
    private static final String URL6 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=dbfd4ebcc0d23aff1n1839W9FlI~&status=0";

    private static final String DIANHUA_SALE = "https://www.zhipin.com/chat/im?mu=recommend&jobid=bfe766ee12cf4a251XNz09y0E1o~&status=0";
    private static final String SALE_PRESENT = "https://www.zhipin.com/chat/im?mu=recommend&jobid=3b3ea7eb843377951XNz09-0ElA~&status=0";
    private static final String ZHAOPIN_ZHUANYUAN = "https://www.zhipin.com/chat/im?mu=recommend&jobid=bbf226b7eafd5da11XNz0921GFI~&status=0";
    private static final String HUGE_AMOUNT_SALE = "https://www.zhipin.com/chat/im?mu=recommend&jobid=4192810b862a60be1XNz0924F1Y~&status=0";
    private static final String GAOXIN_XINDAI = "https://www.zhipin.com/chat/im?mu=recommend&jobid=6a0fa6c0ea29c28a1XNz3NS-GVo~&status=0";
    private static final String PEI_XUN_SHI = "https://www.zhipin.com/chat/im?mu=recommend";

    private static final String newurl_1 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=d3173b711bcc79e21X192N-4FFM~&status=0";
    private static final String newurl_2 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=9bb59f519147f5851X192N-9GFs~&status=0";
    private static final String newurl_3 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=4c9efe8e4c5b47e21X192Ny-E1Y~&status=0";
    private static final String newurl_4 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=3b3ea7eb843377951XNz09-0ElA~&status=0";
    private static final String newurl_5 = "https://www.zhipin.com/chat/im?mu=recommend&jobid=bfe766ee12cf4a251XNz09y0E1o~&status=0";

    private static final String LIS_XPATH = "//*[@id=\"recommend-list\"]/div[1]/ul/li";
    private JavascriptExecutor js;

    private int count = 0; //打招呼次数

    private boolean searchWidely;

    public void enableSearchWidely() {
        searchWidely = true;
    }

    public BossAuto(WebDriver driver) {
        this.driver = driver;
        this.navigation = driver.navigate();
        this.js = (JavascriptExecutor) driver;
    }

    /**
     * 延时
     * @param second 秒
     */
    private void delay(double second){
        try {
            Thread.sleep((long) (second * 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
//        String[] urls = new String[]{DIANHUA_SALE, SALE_PRESENT, newurl_3,newurl_2,newurl_5,newurl_4,newurl_1};
        String[] urls = new String[]{
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=5cc1b8b4101cb4931H172Nq6FFI~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=eb425779c3bf49cc1Hx609i1F1U~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=399a4796733179641Hx63N-9FlY~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=54e5cb3c07ab3acc1HJy0tm1EFY~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=b5e58bbb9cc3fc711HJ_2di_FlM~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=70f9d45a37ffa8e01HF90920FlM~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=239b5bed3b165b701HF-0tm5FFU~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=762575a9a5a72e7c1HF609S4F1E~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=4a6b2d4fa2fd04941HF63tu1FVc~&status=0",
                "https://www.zhipin.com/chat/im?mu=recommend&jobid=54a61375bad122d51HF63ti_F1o~&status=0"
        };
        for (String url : urls) {
            if (count < 400) {
                runForOneUrl(url);
            } else {
                System.out.println("----------打招呼完成-----------");
                break;
            }
        }
        if (searchWidely) { //已经扩大范围打过招呼了
            return;
        }
        // search widely
        searchWidely = true;
        run();
    }

    public void runForOneUrl(String url) {
        navigation.to(url);
        loadAllPages();
        runForOnePage();
    }

    public void loadAllPages() {
        while (!isThereNoData()) {
            delay(0.5);
            List<WebElement> lis = driver.findElements(By.xpath(LIS_XPATH));
            WebElement webElement = driver.findElement(By.xpath(LIS_XPATH + "[" + lis.size() + "]/div/button"));
            js.executeScript("arguments[0].scrollIntoView();", webElement);

            System.out.println(lis.size());
        }
    }

    public void runForOnePage() {
        List<WebElement> lis = driver.findElements(By.xpath(LIS_XPATH));
        if (CollectionUtils.isNotEmpty(lis)) {
            for(int i = 0; i< lis.size(); i++) {
                String lastActiveXpath = LIS_XPATH + "[" + (i + 1) + "]/a/div[1]/span[6]";
                String educationXpath = LIS_XPATH + "[" + (i + 1) + "]/a/div[1]/span[3]";
                String ageXpath = LIS_XPATH + "[" + (i + 1) + "]/a/div[1]/span[4]";
                String workStatusXpath = LIS_XPATH + "[" + (i + 1) + "]/a/div[1]/span[5]";
                String intentXpath = LIS_XPATH + "[" + (i + 1) + "]/a/div[2]/div[3]/p[1]";
                String experienceXpath = LIS_XPATH + "[" + (i + 1) + "]/a/div[2]/div[3]/p[2]";
                String sendBtnXpath = LIS_XPATH + "[" + (i + 1) + "]/div/button[2]";

                WebElement sendBtn = driver.findElement(By.xpath(sendBtnXpath));
                String btnTxt = sendBtn.getText();
                if (!"打招呼".equals(btnTxt)) {
                    continue;
                }
                if (isOk(lastActiveXpath, educationXpath, ageXpath, workStatusXpath, intentXpath, experienceXpath)) {
                        count++;
                        System.out.println("【" + count + "】" + (i + 1) + " is ok clicking...");
                        js.executeScript("arguments[0].scrollIntoView();", sendBtn);
                        js.executeScript("arguments[0].click();", sendBtn);
                        delay(0.1);
                }
            }
        }
    }

    private boolean isOk(String lastActiveXpath, String educationXpath, String ageXpath,
                         String workStatusXpath, String intentXpath, String experienceXpath) {
        if (searchWidely) {
            return isActiveOk(lastActiveXpath) && isEducationOk(educationXpath) && isAgeOk(ageXpath)
                    && isWorkStatusOkInWide(workStatusXpath) && isIntentOk(intentXpath) && isExperienceOk(experienceXpath);
        } else {
            return isActiveOk(lastActiveXpath) && isEducationOk(educationXpath) && isAgeOk(ageXpath)
                    && isWorkStatusOk(workStatusXpath) && isIntentOk(intentXpath) && isExperienceOk(experienceXpath);
        }
    }

    private boolean isExperienceOk(String experienceXpath) {
        return true;
//            WebElement active = driver.findElement(By.xpath(experienceXpath));
//            String activeTxt = active.getText();
//            System.out.println("experience：" + activeTxt);
//            if (StringUtils.isBlank(activeTxt)) {
//                return true;
//            }
//            return !activeTxt.contains("金融") && !activeTxt.contains("银行") && !activeTxt.contains("贷")&& !activeTxt.contains("风控");
    }

    private boolean isIntentOk(String intentXpath) {
        return true;
//            WebElement active = driver.findElement(By.xpath(intentXpath));
//            String activeTxt = active.getText();
//            return StringUtils.isNotBlank(activeTxt) && !activeTxt.contains("经理") && !activeTxt.contains("风控");
    }

    private boolean isEducationOk(String educationXpath) {

            WebElement active = driver.findElement(By.xpath(educationXpath));
            String activeTxt = active.getText();
            return !"硕士".equals(activeTxt) && !"博士".equals(activeTxt);

    }

    private boolean isActiveOk(String lastActiveXpath) {
        try {
            WebElement active = driver.findElement(By.xpath(lastActiveXpath));
            String activeTxt = active.getText();
            return "刚刚活跃".equals(activeTxt) || "今日活跃".equals(activeTxt);
        } catch (Exception e) {
            return false;
        }
    }

    private boolean isAgeOk(String xpath) {
        try {
            WebElement age = driver.findElement(By.xpath(xpath));
            String ageTxt = age.getText();
            String ageStr = ageTxt.split("岁")[0];
            if (StringUtils.isBlank(ageStr)) {
                return false;
            }
            int ageInt = Integer.parseInt(ageStr);
            if (ageInt > 17 && ageInt < 27) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
//            System.out.println("age check error:" + e.getMessage());
            return false;
        }
    }

    private boolean isWorkStatusOk(String xpath) {
            WebElement workStatus = driver.findElement(By.xpath(xpath));
            String workStatusTxt = workStatus.getText();
            return "离职-随时到岗".equals(workStatusTxt);
    }

    private boolean isWorkStatusOkInWide(String xpath) {
        WebElement workStatus = driver.findElement(By.xpath(xpath));
        String workStatusTxt = workStatus.getText();
        return "离职-随时到岗".equals(workStatusTxt) || "在职-考虑机会".equals(workStatusTxt)
                || "在职-月内到岗".equals(workStatusTxt);
    }

    private boolean isThereNoData() {
        String xpath = "//*[@id=\"recommend-list\"]/div[1]/div[2]";
        try {
            WebElement element = driver.findElement(By.xpath(xpath));
            return "没有更多了".equals(element.getText());
        } catch (Exception e) {
            System.out.println("是否没有数据了 error:" + e.getMessage());
            return false;
        }
    }

}
